﻿#ifndef _DB_IDATASOURCEFACTORY_H
#define _DB_IDATASOURCEFACTORY_H

#include "udatabase/interface/idatasource.h"
#include "kernel/Referenced.h"
#include "udatabase/dll_cfg.h"

namespace FD
{
	namespace Utility
	{
		namespace DB
		{
			namespace Interface
			{
				class IPropertySet;				

				/// <summary>
				/// 数据源工厂接口
				/// </summary>
				class FD_DB_Export IDataSourceFactory :  public FD::Kernel::CReferenced
				{
				public:			
					/// <summary>
					/// 默认析构函数
					/// </summary>
					/// <returns></returns>
					virtual ~IDataSourceFactory();
					/// <summary>
					/// 返回对应的数据源类型
					/// </summary>
					/// <param name=""></param>
					/// <returns>数据源类型</returns>
					virtual INT32 getType() const;
					/// <summary>
					/// 从XML创建数据源对象
					/// </summary>
					/// <param name="xml">xml字符串</param>
					/// <returns>数据源对象</returns>
					virtual FD::Utility::DB::Interface::IDataSource* createFromXML(const String& xml);
					/// <summary>
					/// 打开一个存在的数据源
					/// </summary>
					/// <param name="path">路径</param>
					/// <param name="fileName">文件名称</param>
					/// <returns>数据源对象</returns>
					virtual FD::Utility::DB::Interface::IDataSource * openDataSource(const String& path,const String& fileName);
					/// <summary>
					/// 打开一个存在的数据源
					/// </summary>
					/// <param name="connection">连接字符对</param>
					/// <returns>数据源对象</returns>
					virtual FD::Utility::DB::Interface::IDataSource * openDataSource(FD::Utility::DB::Interface::IPropertySet * connection);
					/// <summary>
					/// 创建一个数据源
					/// </summary>
					/// <param name="path">路径</param>
					/// <param name="fileName">文件名称</param>
					/// <returns>数据源对象</returns>
					virtual FD::Utility::DB::Interface::IDataSource * createDataSource(const String& path,const String& fileName);
					/// <summary>
					/// 创建一个数据源
					/// </summary>
					/// <param name="connection">连接字符对</param>
					/// <returns>数据源对象</returns>
					virtual FD::Utility::DB::Interface::IDataSource * createDataSource(FD::Utility::DB::Interface::IPropertySet * connection);
					/// <summary>
					/// 销毁一个数据源对象
					/// </summary>
					/// <param name="pDataSource">数据源对象</param>
					/// <returns></returns>
					virtual void destroyDataSource(FD::Utility::DB::Interface::IDataSource* pDataSource);
					/// <summary>
					/// 判断一个数据源是否存在
					/// </summary>
					/// <param name="pDataSource">数据源对象</param>
					/// <returns></returns>
					virtual bool existDataSource(FD::Utility::DB::Interface::IDataSource* pDataSource) const;

				protected:
					/// <summary>
					/// 默认构造函数
					/// </summary>
					/// <returns></returns>
					IDataSourceFactory();
				private:
					C_DISABLE_COPY(IDataSourceFactory);
				};

				/// <summary>
				/// 数据源创建工厂容器
				/// </summary>
				class FD_DB_Export CDataSourceFactoryEnumerator : public FD::Kernel::CReferenced
				{
				public:
					/// <summary>
					/// 默认够着函数
					/// </summary>
					/// <returns></returns>
					CDataSourceFactoryEnumerator();
					/// <summary>
					/// 默认析构函数
					/// </summary>
					/// <returns></returns>
					~CDataSourceFactoryEnumerator();

				public:
					/// <summary>
					/// 静态函数，获取对象单件
					/// </summary>
					/// <returns></returns>
					static FD::Utility::DB::Interface::CDataSourceFactoryEnumerator* getSingletonPtr();
					/// <summary>
					/// 静态函数，获取对象单件
					/// </summary>
					/// <returns></returns>
					static FD::Utility::DB::Interface::CDataSourceFactoryEnumerator& getSingleton();
				protected:
					static FD::Utility::DB::Interface::CDataSourceFactoryEnumerator* mpSingleton;

				public:
					/// <summary>
					/// 是否存在指定类型的数据源创建工厂
					/// </summary>
					/// <param name="typeOfDataSource">指定数据源类型，用INT表示</param>
					/// <returns>ture，已经存在；false，不存在</returns>
					bool existDataSourceFactory(INT32 typeOfDataSource);
					/// <summary>
					/// 获取指定类型的数据源创建工厂
					/// </summary>
					/// <param name="typeOfDataSource">指定数据源类型，用INT表示</param>
					/// <returns>数据源工厂对象</returns>
					IDataSourceFactory* getDataSourceFactory(INT32 typeOfDataSource);
					/// <summary>
					/// 添加数据源创建工厂
					/// </summary>
					/// <param name="factory">数据源工厂对象</param>
					/// <returns>true，成功添加；false，添加不成功</returns>
					bool addDataSourceFactory(IDataSourceFactory* ref_factory);
					/// <summary>
					/// 移除指定类型的数据源创建工厂
					/// </summary>
					/// <param name="typeOfDataSource">指定数据源类型，用INT表示</param>
					/// <returns>true，成功移除；false，移除不成功</returns>
					bool removeDataSourceFactory(INT32 typeOfDataSource);
					/// <summary>
					/// 移除数据源创建工厂
					/// </summary>
					/// <param name="factory">数据源工厂对象</param>
					/// <returns>true，成功移除；false，移除不成功</returns>
					bool removeDataSourceFactory(IDataSourceFactory* factory);
					/// <summary>
					/// 移除所有数据源创建工厂
					/// </summary>
					/// <returns></returns>
					void clearDataSourceFactory();

				protected:
					typedef std::list<IDataSourceFactory*> DataSourceFactorys;
					DataSourceFactorys mFactorys;
				};
			}
		}
	}
}

#endif
